package com.situ.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.mall.mapper.ProductMapper;
import com.situ.mall.pojo.Product;
import com.situ.mall.pojo.query.ProductQuery;
import com.situ.mall.service.IProductService;
import com.situ.mall.util.PageResult;
import exception.MyException;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.thymeleaf.spring6.processor.SpringInputGeneralFieldTagProcessor;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RedissonClient redissonClient;

    @Override
    public PageResult<Product> list(ProductQuery productQuery) {
        PageHelper.startPage(productQuery.getPage(),productQuery.getLimit());
        List<Product> list = productMapper.list(productQuery);
        PageInfo pageInfo = new PageInfo(list);
        int count = (int)pageInfo.getTotal();
//        if(count>0){
//            throw new MyException("自定义异常");
//        }
        return new PageResult<>(0,"",count,list);
    }

//    public Product selectById(Integer id) {
//        Product product =(Product) redisTemplate.opsForValue().get("product:"+id);
//        if(product==null){
//            synchronized (this){
//                product =(Product) redisTemplate.opsForValue().get("product:"+id);
//                    if(product==null){
//                        product = productMapper.selectByPrimaryKey(id);
//                        if(product!=null){
//                            redisTemplate.opsForValue().set("product:"+id,product,1,TimeUnit.DAYS);
//                        }
//                        else{
//                            redisTemplate.opsForValue().set("product:"+id,new Product(),1, TimeUnit.MINUTES);
//                        }
//                    }
//                }
//            }
//        return product;
//    }

    public Product selectById(Integer id){
        Product product = (Product) redisTemplate.opsForValue().get("product:"+id);
        if(product == null){
            redissonClient.getLock("product_lock:"+id).lock();
            try{
                product = (Product) redisTemplate.opsForValue().get("product:"+id);
                if(product == null){
                    product=productMapper.selectByPrimaryKey(id);
                    if(product != null){
                        redisTemplate.opsForValue().set("product:"+id,product,1,TimeUnit.DAYS);
                    }else{
                        redisTemplate.opsForValue().set("product:"+id,new Product(),1,TimeUnit.MINUTES);
                    }
                }
            }finally {
                redissonClient.getLock("product_lock:"+id).unlock();
            }
        }
        return product;
    }
}
